﻿using System;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Tab2Sql
{
    /// <summary>
    ///
    /// </summary>
    public class SqlSnippets
    {
        XDocument ourXmlDoc;
        /// <summary>
        ///
        /// </summary>
        public string NameList;
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="param1">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="param2">
        /// A <see cref="System.String"/>
        /// </param>
        public SqlSnippets(string fileName, string param1, string param2)
        {
            NameList = "";
            ourXmlDoc = getXmlDoc(fileName);
            var xmlNodes = ourXmlDoc.Descendants();
            foreach (var foo in xmlNodes)
            {
                if (!foo.IsEmpty)
                {
                    //NameList += "---------------------------------->" + foo.Name;
                    if (foo.Element("code") != null)
                    {
                        NameList += foo.Element("code").Value;
                    }
                }
            }
            NameList = NameList.Replace(param1, param2);
        }



        private XDocument getXmlDoc(string sFile)
        {
            XDocument theDoc = XDocument.Load(sFile);
            return theDoc;
        }

    }


/// <summary>
///
/// </summary>

    internal static class sqlFragments
    {
     
        /// <summary>
        /// Inserts a "DROP TABLE" command within error catch.
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string dropTable(string tableName)
        {
            string ProperTab = "    ";
            int len = tableName.Trim().Length;
            
            string tmpy;
            if (tableName.StartsWith("#"))
            {
               
                tmpy = "if exists (select  * from tempdb.dbo.sysobjects o   where o.xtype in ('U') and o.id = object_id(N'tempdb.."+tableName.Trim()+"'))";
                tmpy += Environment.NewLine;
                tmpy += "BEGIN";
                tmpy += Environment.NewLine;
                tmpy += ProperTab + "BEGIN TRY"+Environment.NewLine;
                tmpy += DGen.DText.Replicate(ProperTab,2) + "DROP TABLE " + tableName + " ;"+Environment.NewLine;
                tmpy += ProperTab + "END TRY" + Environment.NewLine;
                tmpy += ProperTab + "BEGIN CATCH" + Environment.NewLine;
                tmpy += DGen.DText.Replicate(ProperTab, 2) + "PRINT 'Drop Failed'"+Environment.NewLine;
                tmpy += ProperTab + "END CATCH" + Environment.NewLine;
                tmpy += "END";
                tmpy += Environment.NewLine;
                tmpy += "GO";
                tmpy += Environment.NewLine;
            }
            else
            {
                tmpy = "BEGIN TRY" + Environment.NewLine + "\t" + "DROP TABLE " + tableName + ";";
                tmpy += Environment.NewLine + "END TRY";
                tmpy += Environment.NewLine;
                tmpy += "BEGIN CATCH" + Environment.NewLine + "END CATCH";

            }

            return tmpy;


        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="tableName">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="variant">
        /// A <see cref="SqlVariants"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public static string dropTable(string tableName,SqlVariants variant)
        {
            string tmpy = String.Empty;
            if (variant == SqlVariants.TSql)
            {
                return dropTable(tableName);
            }
            if (variant == SqlVariants.SqlAnywhere)
            {
                tmpy = "BEGIN" + Environment.NewLine + "DROP TABLE " + tableName + ";" + Environment.NewLine+ "EXCEPTION WHEN OTHERS THEN" + Environment.NewLine + "END;";
                return tmpy;
            }
            else
            {
                tmpy = "--DROP TABLE " + tableName + ";";
            }

            return tmpy;


        }


        /// <summary>
        ///
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public static string setDateFormat()
        {
            return "SET DATEFORMAT DMY;" + Environment.NewLine;
        }


        /// <summary>
        /// SET NCOUNT ON;
        /// </summary>
        /// <returns></returns>
        public static string setnocount()
        {
            return "SET NOCOUNT ON;" + Environment.NewLine;
        }

        /// <summary>
        /// Returns header text suitable for file output. 
        /// </summary>
        /// <returns></returns>
        public static string getPreamble()
        {
            var asm = new Assemblage();


            string dateTime = "00/00/0000";
            dateTime = DateTime.Now.ToShortDateString();
            string build = asm.AssemblyVersion;
            string preamble =
                @"
/***************************************************************
******		SQL statements generated by Tab2Sql 		  ******
******		Build Version 	: " +
                build + @" 			  ******
******		Script Date		: " + dateTime +
                @"			   	  ******	 
****************************************************************/";
            asm = null;
            return preamble;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public static string dupeCheck()
        {
            return "NOT IMPLEMENTED";
        }
    }
}